<?php


namespace app\api\controller\v15;


use app\api\controller\v2\Auth;
use app\common\model\UserPush;

class Push extends Auth
{
//    protected $noteice = [
//        0 => [
//            'name' => '运势提醒通知',
//            'type' => 1
//        ],
//        1 => [
//            'name' => '天象变动通知',
//            'type' => 2
//        ],
//        2 => [
//            'name' => '生日提醒通知',
//            'type' => 3
//        ]
//    ];

    /**
     * 通知类型
     * @var array
     */
    protected $notice_type = [3];

    /**
     * 通知数据
     * @var array
     */
    protected $notice = [
        0 => [
            'name' => '生日提醒通知',
            'type' => 3
        ]
    ];

    /**
     * 获取通知状态
     * @return \think\response\Json
     * @throws \Exception
     */
    public function notice()
    {
        $userPushModel = new UserPush();
        //获取用户推送信息
        $userPush = $userPushModel->getUserRegid((int)$this->user['id']);
        $data = [];
        foreach ($this->notice as $item)
        {
            if ($userPush)
            {
                $item['status'] = (int)$userPush['birth_status'];
            } else
            {
                $item['status'] = 1;
            }
            $data[] = $item;
        }
        return show(config('code.success'), '获取成功', $data);
    }

    /**
     * 推送通知开关控制
     * @return \think\response\Json
     * @throws \app\common\lib\exception\ApiException
     */
    public function changeNotice()
    {
        $type = input('type/d', 0);
        $status = input('status/d', 1);
        if (!$type || !in_array($type, $this->notice_type, true))
        {
            return show(config('code.success'), '未知的通知类型');
        }
        $userPushModel = new UserPush();
        if ($type === 3)
        {
            $data = [
                'birth_status' => $status
            ];
        } else
        {
            $data = [];
        }
        if ($data)
        {
            $userPushModel->pushBind($data, (int)$this->user['id'], $type, $status);
        }
        return show(config('code.success'), '操作成功', ['type' => $type, 'status' => $status]);
    }
}
